package com.yanqu.road.dao.impl.activity.jiuzhou.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouPatronsData;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouUserData;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouUserRoundRecordData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJiuZhouUserDataDaoImpl extends TempDataObjectDao<CrossJiuZhouUserData> {
    
    public static String getCrossJiuZhouUserDataSql = "select * from `t_c_jiu_zhou_user_data` where `activityId`=? ";

    public Map<Long, CrossJiuZhouUserData> getCrossJiuZhouUserDataMap(int activityId) {
        Map<Long, CrossJiuZhouUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossJiuZhouUserDataSql = "update `t_c_jiu_zhou_user_data` set roomIdMap=?,patronsAbility=?,zoneType=?,lastRoundId=?,recordDataMap=?,patronsDataMap=?,popularityMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossJiuZhouUserData crossJiuZhouUserData) {
        crossJiuZhouUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getRoomIdMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouUserData.getPatronsAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getZoneType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getLastRoundId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getRecordDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getPatronsDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getPopularityMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouUserDataSql = "insert into `t_c_jiu_zhou_user_data`(activityId,userId,roomIdMap,patronsAbility,zoneType,lastRoundId,recordDataMap,patronsDataMap,popularityMap) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouUserData crossJiuZhouUserData) {
        crossJiuZhouUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getRoomIdMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouUserData.getPatronsAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getZoneType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserData.getLastRoundId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getRecordDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getPatronsDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserData.getPopularityMap())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouUserData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouUserData data = new CrossJiuZhouUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        String roomIdMap = rs.getString("roomIdMap");
        if (!StringUtils.isNullOrEmpty(roomIdMap)) {
            data.setRoomIdMap(JSON.parseObject(roomIdMap, new TypeReference<Map<Integer, Integer>>() {
            }));
        }
        String patronsAbility = rs.getString("patronsAbility");
        if (!StringUtils.isNullOrEmpty(patronsAbility)) {
            data.setPatronsAbility(new BigInteger(patronsAbility));
        }else {
            data.setPatronsAbility(new BigInteger("0"));
        }

        data.setZoneType(rs.getInt("zoneType"));
        data.setLastRoundId(rs.getInt("lastRoundId"));
        String recordDataMap = rs.getString("recordDataMap");
        if (!StringUtils.isNullOrEmpty(recordDataMap)) {
            data.setRecordDataMap(JSON.parseObject(recordDataMap, new TypeReference<ConcurrentHashMap<Integer, CrossJiuZhouUserRoundRecordData>>() {
            }));
        }
        String patronsDataMap = rs.getString("patronsDataMap");
        if (!StringUtils.isNullOrEmpty(patronsDataMap)) {
            data.setPatronsDataMap(JSON.parseObject(patronsDataMap, new TypeReference<ConcurrentHashMap<Integer, CrossJiuZhouPatronsData>>() {
            }));
        }
        String popularityMap = rs.getString("popularityMap");
        if (!StringUtils.isNullOrEmpty(popularityMap)) {
            data.setPopularityMap(JSON.parseObject(popularityMap, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        return data;
    }
}
